# -*-coding:utf-8-*-

from pyrestful import mediatypes
from pyrestful.rest import get
from handlers.base import BaseHandler
from utils.auth import authenticated
from utils.sign import signed
from tornado.gen import coroutine, Return

entries = {
    'group_id': '8',
    'mq_entry': {
        #'address': '47.104.26.62:1883',
        'address': 'mq.hifocus.top:1883',
        'auth': 'hifocus:519070'
    },
    'stun_entry': {
        'address': 'IP:PORT',
        'auth': 'USERNAME:PASSWORD'
    },
    'turn_entry': {
        'address': 'IP:PORT',
        'auth': 'USERNAME:PASSWORD'
    },
}


class EntriesHandler(BaseHandler):
    def __init__(self, application, request, **kwargs):
        super(EntriesHandler, self).__init__(application, request, **kwargs)

    @coroutine
    def check_device_id(self, device_id):
        result = yield self.devices_dao.is_device_exists(device_id)
        if not result:
            self.respond_error('invalid_device_id')

    @get(_path='/api/users/{user_id}/entries', _types=[str], _produces=mediatypes.APPLICATION_JSON)
    @authenticated
    @coroutine
    def user_get_entries(self, user_id):
        if user_id != self.user_id:
            self.respond_error('invalid_path', 'user id not match')
        self.respond_success(entries)

    # TODO: 根据设备/产品类型不同，返回不一样的 entries 信息
    @get(_path='/api/devices/{device_id}/entries', _types=[str], _produces=mediatypes.APPLICATION_JSON)
    # @signed(sign_from_body=False)
    @coroutine
    def device_get_entries(self, device_id):
        yield self.check_device_id(device_id)
        self.respond_success(entries)
